package com.cfgj.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cfgj.entity.CrowdFunding;
import com.cfgj.entity.Image;
import com.cfgj.entity.Projects;
import com.cfgj.mapper.ContractMapper;
import com.cfgj.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/home")
@Controller
public class HomeController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ContractMapper contractMapper;
    //用户展示
    @RequestMapping("/member_list")
    public String member_list(){
        return "admin/member-list";
    }

    @RequestMapping("/member_del")
    public String member_del(){
        return "admin/member-del";
    }

    /**
     * 提额
     */
    @RequestMapping("/addUserMoney")
    public String addUserMoney(Model model,Integer id,double money) {
        model.addAttribute("id",id);
        model.addAttribute("money",money);
        return "admin/addUserMoney";
    }

    /**
     * 提额成功
     */
    @RequestMapping("/addUserMoneySubmit")
    @ResponseBody
    public String addUserMoneySubmit(Projects project) {
        //System.out.println(projects);
        projectService.updateProjectUserMoneyByUserId(project);
        return "success";

    }


    //项目审核
    @RequestMapping("/project_examine")
    public String project_examine(Model model,Integer page,Integer size){
       // List<Projects> list=projectService.findStepZero();
        IPage<Projects> ipage=projectService.findPageList(page,size,0);
        Integer num = projectService.findStepNum(0);
        model.addAttribute("examines",ipage.getRecords());
        model.addAttribute("count",ipage.getTotal());
        model.addAttribute("curr",page);
        model.addAttribute("num",num);
        return "admin/project-examine";
    }

    //项目借款中
    @RequestMapping("/project_loan")
    public String project_list(Model model,Integer page,Integer size){
        //List<Projects> list=projectService.findStepOne();
        IPage<Projects> ipage=projectService.findPageList(page,size,1);
        Integer num = projectService.findStepNum(1);
        model.addAttribute("loans",ipage.getRecords());
        model.addAttribute("count",ipage.getTotal());
        model.addAttribute("curr",page);
        model.addAttribute("num",num);
        return "admin/project-loan";
    }

    //项目还款中
    @RequestMapping("/project_Repayments")
    public String project_Repayments(Model model,Integer page,Integer size){
        IPage<Projects> ipage=projectService.findPageList(page,size,2);
        Integer num = projectService.findStepNum(2);
        model.addAttribute("Repayments",ipage.getRecords());
        model.addAttribute("count",ipage.getTotal());
        model.addAttribute("curr",page);
        model.addAttribute("num",num);
        return "admin/project-Repayments";
    }

    //项目已完成
    @RequestMapping("/project_completion")
    public String project_completion(Model model,Integer page,Integer size){
        IPage<Projects> ipage=projectService.findPageList(page,size,3);
        Integer num = projectService.findStepNum(3);
        model.addAttribute("completions",ipage.getRecords());
        model.addAttribute("count",ipage.getTotal());
        model.addAttribute("curr",page);
        model.addAttribute("num",num);
        return "admin/project-completion";
    }

    //更新状态
    @RequestMapping("/project_update_step")
    @ResponseBody
    public Integer project_update_step(Integer id){
        projectService.updateProjectStepZero(id);
        return 1;
    }

    //更新状态
    @RequestMapping("/project_update_stepThird")
    public Integer project_update_stepThird(Integer id){
        projectService.updateProjectStepZeroThird(id);
        return 3;
    }

    //展示信息
    @RequestMapping("/project_show")
    public String project_show(Integer id,Model model){
        System.out.println(id);
        List<Image> car = projectService.FindCarList(id);
        List<Image> home = projectService.FindHomeList(id);
        List<Image> idCard = projectService.FindIdCardList(id);
        List<Image> others = projectService.FindOtherList(id);
        System.out.println(car);
        System.out.println(home);
        System.out.println(idCard);
        System.out.println(others);
        model.addAttribute("credit_id",id);
        model.addAttribute("cars",car);
        model.addAttribute("homes",home);
        model.addAttribute("idCards",idCard);
        model.addAttribute("others",others);
        return "admin/project_show";
    }





    //众筹审核

    @RequestMapping("/crowdfunding_examine")
    public String crowdfunding_examine(){
        return "admin/crowdfunding-examine";
    }


    //下载文件
    @RequestMapping("/downloadOnlineLearnMaterials")
    public String downloadFile(Integer credit_id,HttpServletRequest request, HttpServletResponse response) {
        String fileName = "p2pLoanagreement.pdf";// 设置文件名，根据业务需要替换成要下载的文件名
        String fileUrl=contractMapper.getContractByCreditId(credit_id);
        int c=fileUrl.lastIndexOf("/")+1;
        String newFileUrl=fileUrl.substring(c);
        String path=fileUrl.substring(0,fileUrl.lastIndexOf("/"));
        if (newFileUrl != null) {
            //设置文件路径
            String realPath = "F://picall//cfgj//"+path;
            File file = new File(realPath , newFileUrl);//设置文件路径和名字
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + newFileUrl);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }
}
